package com.tangfuzhong.test;

import java.util.Map;

import com.tangfuzhong.util.MyApplicationAware;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import com.tangfuzhong.bean.Person;
import com.tangfuzhong.config.Config02_2Bean_Scope_Lazy_Condition_Import_FactoryBean;

public class IOCTest_02_2Bean_Scope_Lazy_Condition_Import_FactoryBean {


	
	//@Import({Color.class,Red.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
	@Test
	public void testFactoryBean(){

		AnnotationConfigApplicationContext applicationContext
				= new AnnotationConfigApplicationContext(Config02_2Bean_Scope_Lazy_Condition_Import_FactoryBean.class);
		MyApplicationAware.printBeans(applicationContext);

		//容器中没有Blue.class,会抛出异常
		//Blue bean = applicationContext.getBean(Blue.class);
		//System.out.println(bean);
		
		//工厂Bean获取的是调用getObject创建的对象
		Object bean2 = applicationContext.getBean("colorFactoryBean");
		Object bean3 = applicationContext.getBean("colorFactoryBean");
		System.out.println("bean的类型："+bean2.getClass());
		System.out.println(bean2 == bean3);
		
		Object bean4 = applicationContext.getBean("&colorFactoryBean");
		System.out.println(bean4.getClass());
	}
	

	
	@Test
	public void test03(){

		AnnotationConfigApplicationContext applicationContext
				= new AnnotationConfigApplicationContext(Config02_2Bean_Scope_Lazy_Condition_Import_FactoryBean.class);
		String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
		ConfigurableEnvironment environment = applicationContext.getEnvironment();
		//动态获取环境变量的值；Windows 10
		String property = environment.getProperty("os.name");
		System.out.println(property);
		for (String name : namesForType) {
			System.out.println(name);
		}
		
		Map<String, Person> persons = applicationContext.getBeansOfType(Person.class);
		System.out.println(persons);
		
	}
	
	@Test
	public void test02(){

		AnnotationConfigApplicationContext applicationContext
				= new AnnotationConfigApplicationContext(Config02_2Bean_Scope_Lazy_Condition_Import_FactoryBean.class);

//		String[] definitionNames = applicationContext.getBeanDefinitionNames();
//		for (String name : definitionNames) {
//			System.out.println(name);
//		}
//		
		System.out.println("ioc容器创建完成....");
		Object bean = applicationContext.getBean("person");
		Object bean2 = applicationContext.getBean("person");
		System.out.println(bean == bean2);
	}


}
